package com.wmx.newspushmonitor;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.rhino.ui.utils.ApkUtils;
import com.rhino.ui.utils.LogUtils;
import com.rhino.ui.utils.SharedPreferencesUtils;
import com.rhino.ui.utils.ui.ColorUtils;
import com.wmx.newspushmonitor.base.BaseSimpleTitleHttpActivity;
import com.wmx.newspushmonitor.databinding.ActivityLoginBinding;
import com.wmx.newspushmonitor.http.HttpUrl;
import com.wmx.newspushmonitor.http.request.LoginParams;
import com.wmx.newspushmonitor.util.UiUtils;

/**
 * @author LuoLin
 * @since Create on 2019/2/22.
 */
public class LoginActivity extends BaseSimpleTitleHttpActivity<ActivityLoginBinding> {
    @Override
    public void setContent() {
        setContentView(R.layout.activity_login);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    }

    @Override
    public void initView() {
        mActionBarHelper.setBackgroundColor(ColorUtils.WHITE);
        mActionBarHelper.setTitleVisible(false);
        dataBinding.tvVersion.setText("版本" + ApkUtils.getAppVersionName(getApplicationContext())
                + "(" + ApkUtils.getAppVersionCode(getApplicationContext()) + ")");
        setBaseOnClickListener(dataBinding.btLogin, dataBinding.chRememberPassword,
                dataBinding.chAutoLogin);
        if (BuildConfig.DEBUG) {
            setBaseOnClickListener(dataBinding.tvVersion);
            dataBinding.etAccount.setText("1424440105");
            dataBinding.etPassword.setText("1234567");
        }
        String uaccount = SharedPreferencesUtils.getInstance().getString("user_account");
        if (!TextUtils.isEmpty(uaccount)) {
            dataBinding.etAccount.setText(uaccount);
        }
        if (SharedPreferencesUtils.getInstance().getBoolean("remember_password", false)) {
            dataBinding.etPassword.setText(SharedPreferencesUtils.getInstance().getString("user_password"));
        }
        dataBinding.chRememberPassword.setSelected(SharedPreferencesUtils.getInstance().getBoolean("remember_password", false));
        dataBinding.chAutoLogin.setSelected(SharedPreferencesUtils.getInstance().getBoolean("auto_login", false));
    }

    @Override
    public void baseOnClickListener(View v) {
        super.baseOnClickListener(v);
        if (v == dataBinding.btLogin) {
            onClickLoginByUserPwd();
        } else if (v == dataBinding.chRememberPassword) {
            onClickRememberPassword();
        } else if (v == dataBinding.chAutoLogin) {
            onClickAutoLogin();
        }  else if (v == dataBinding.tvVersion) {
        }
    }

    @Override
    public void callbackHandler(int event, String url, String data, Object reqData) {
        super.callbackHandler(event, url, data, reqData);
        LogUtils.d("data = " + data);
        if (event == EVENT_FAILED) {
            dismissLoadingDialog();
            showToast("登录失败，请求异常");
        } else if (event == EVENT_SUCCESS) {
            switch (url) {
                case HttpUrl.URL_LOGIN:
                    dismissLoadingDialog();
                    if ("0".equals(data)) {
                        showToast("账号或密码错误！");
                        return;
                    }
                    SharedPreferencesUtils.getInstance().putString("user_account", ((LoginParams)reqData).account);
                    SharedPreferencesUtils.getInstance().putString("user_password", ((LoginParams)reqData).pwd);
                    SharedPreferencesUtils.getInstance().putString("token", data);
                    UiUtils.showMainPage(this);
                    finish();
                    break;
                default:
                    break;
            }
        }
    }

    private void onClickLoginByUserPwd() {
        String account = dataBinding.etAccount.getText().toString();
        String password = dataBinding.etPassword.getText().toString();
        if (TextUtils.isEmpty(account)) {
            showToast("请输入账号");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            showToast("请输入密码");
            return;
        }
        showLoadingDialog("登录中，请稍后");
        LoginParams p = LoginParams.normal(account, password);
        doPost(HttpUrl.URL_LOGIN, p, p);
    }

    private void onClickRememberPassword() {
        boolean rememberPassword = dataBinding.chRememberPassword.isSelected();
        SharedPreferencesUtils.getInstance().putBoolean("remember_password", !rememberPassword);
        dataBinding.chRememberPassword.setSelected(!rememberPassword);
    }

    private void onClickAutoLogin() {
        boolean autoLogin = dataBinding.chAutoLogin.isSelected();
        SharedPreferencesUtils.getInstance().putBoolean("auto_login", !autoLogin);
        dataBinding.chAutoLogin.setSelected(!autoLogin);
    }

}
